<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/1/19
 * Time: 09:08
 */

namespace app\admin\controller;


use app\common\controller\Backend;
use app\common\utils\Constant;

/**
 * 用户管理
 * Class User
 * @package app\admin\controller
 */
class User extends Backend
{
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where(array('user_type'=>Constant::USER_TYPE_GENERAL))
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->where(array('user_type'=>Constant::USER_TYPE_GENERAL))
                ->field(['password', 'salt', 'token'], true)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key=>&$value){
                if(!empty($value['temple_id'])){
                    $value['temple_name'] = model('Temple')->where(array('id'=>$value['temple_id']))->value('name');
                }else{
                    $value['temple_name'] = '';
                }
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 签列表
     * @param string $ids
     * @return string|\think\response\Json
     */
    public function note_list($ids=''){
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $note_ids_list = model('UserNote')
                ->where(array('user_id'=>$ids))
                ->select();
            $note_ids = [];
            foreach ($note_ids_list as $key=>$value){
                $note_ids[]= $value['note_id'];
            }
            $total = model('Note')
                ->where($where)
                ->where('id','in',$note_ids)
                ->order($sort, $order)
                ->count();
            $list = model('Note')
                ->where($where)
                ->where('id','in',$note_ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key=>&$value){
                $value['temple_name'] = model('Temple')->where(array('id'=>$value['temple_id']))->value('name');
                $solve_note_ids = $value['solve_note_ids'];
                $solve_note_list = model('SolveNote')->where('id','in',$solve_note_ids)->select();
                $solve_note_name_list =[];
                foreach ($solve_note_list as $k=>$v){
                    $solve_note_name_list[]=$v['name'];
                }
                $content_list = model('Content')
                    ->where(array('note_id'=>$value['id']))
                    ->select();
                $content_array = [];
                foreach ($content_list as $k=>$content){
                    $content_array[]=$content['content'];
                }
                $value['solve_note_text'] = implode(',', $solve_note_name_list);
                $value['content_text'] = implode(',', $content_array);
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list,'aaa'=>$ids);
            return json($result);
        }
        $this->assignconfig('user_id',$ids);
        return $this->view->fetch();
    }

    /**
     * 跳转到绑定寺庙界面
     * @param string $ids
     * @return string
     */
    public function bind_temple($ids=''){
        if($this->request->isPost()){
            $temple_id = $this->request->post('temple_id');
            model('User')->where(array('id'=>$ids))->setField('temple_id',$temple_id);
            $this->success('绑定成功！');
        }
        $user = model('User')->where(array('id'=>$ids))->field('temple_id,nickname,id')->find();
        $temple_list = model('Temple')->where(array('is_delete'=>0))->select();
        $temple_name_list = [];
        foreach ($temple_list as $key=>$value){
            $temple_name_list[$value['id']] = $value['name'];
        }
        $this->view->assign('user',$user);
        $this->view->assign('temple_list',$temple_name_list);
        return $this->view->fetch();
    }

}